package com.zeyi.chat.handler;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.annotation.Mapping;
import com.zeyi.chat.controller.WindowMove;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.StageManager;
import com.zeyi.chat.manager.UserManager;
import com.zeyi.chat.po.User;
import com.zeyi.chat.utils.FXMLUtil;
import com.zeyi.chat.views.Alert;
import com.zeyi.chat.views.CircleBorder;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * @author zeyi
 */
public class UserHandler {
    private UserHandler() {
    }

    private static class Singleton {
        public static final UserHandler INSTANCE = new UserHandler();
    }

    public static UserHandler getInstance() {
        return UserHandler.Singleton.INSTANCE;
    }

    //处理登录返回信息
    @Mapping("login")
    public void handleLogin(Message message) {
        if (message.getFlag()) {
            User user = JSON.parseObject(message.getData(), User.class);
            UserManager.setCurrentUser(user);
            Platform.runLater(() -> {
                Stage stage = StageManager.getPrimaryStage();
                Scene scene = FXMLUtil.createScene("/Fxml/Home.fxml");
                new CircleBorder(scene, 5, "#DCDFE6");
                new WindowMove(stage, scene.getRoot());
                stage.setScene(scene);
                stage.show();
            });
        } else {
            new Alert(message.getData(), StageManager.getPrimaryStage());
        }
    }

    //处理注册返回信息
    @Mapping("register")
    public void handleRegister(Message message) {
        if (message.getFlag()) {
            new Alert("注册成功", StageManager.getPrimaryStage());
        } else {
            new Alert(message.getData(), StageManager.getPrimaryStage());
        }
    }

    //处理返回邮件状态
    @Mapping("email")
    public void handleEmail(Message message) {
        new Alert(message.getData(), StageManager.getPrimaryStage());
    }
}
